package com.hfjyz.expert.expertcall.dao;

import java.util.ArrayList;
import java.util.List;

import com.hfjyz.base.bean.SearchCondition;
import com.hfjyz.base.dao.PagingHibernateCallback;
import com.hfjyz.base.entity.CallEntity;
import com.hfjyz.base.entity.ExpertEntity;
import com.hfjyz.base.util.BaseUtil;
import com.hfjyz.call.dao.CallDao;
import com.hfjyz.expert.expertcall.entity.ExpertCallEntity;

public class ExpertCallDao extends CallDao {

    @Override
    public int getType() {
        return 1;
    }

    @SuppressWarnings("unchecked")
    public ExpertEntity findTargetById(Integer targetId) {
        String hql = "from ExpertEntity where id=?";
        List<ExpertEntity> expertList = (List<ExpertEntity>) hibernateTemplate.find(hql, targetId);
        
        if (expertList.size() > 0) {
            return expertList.get(0);
        } else {
            return null;
        }
    }
    
    @SuppressWarnings("unchecked")
    public List<ExpertCallEntity> findCall(SearchCondition cond, String whereClause, List<Object> paramList) {
        String hql = "from CallEntity call, ExpertEntity expert";
        Object[] values = null;
        if (!BaseUtil.isEmpty(whereClause)) {
            hql += whereClause;
            hql += " and call.callType=?";
            paramList.add(getType());
            values = paramList.toArray();
        } else {
            hql += " where call.callType=?";
            values = new Object[] { getType() };
        }
        
        hql += " and call.targetId = expert.id ";
        hql += cond.getOrderString("call", "expert");
        PagingHibernateCallback callback = new PagingHibernateCallback(hql, cond.getPageNum(),
                cond.getRowCountPerPage(), values);
        List<Object> result = hibernateTemplate.executeFind(callback);
        
        List<ExpertCallEntity> list = new ArrayList<ExpertCallEntity>();
        if (result == null || result.isEmpty()) {
            return list;
        }

        for (Object obj : result) {
            Object[] o = (Object[]) obj;
            CallEntity call = (CallEntity)o[0];
            ExpertEntity expert = (ExpertEntity)o[1];
            ExpertCallEntity e = new ExpertCallEntity();
            e.setCallName(call.getCallName());
            e.setCallReason(call.getCallReason());
            e.setCallRemark(call.getCallRemark());
            e.setCallType(getType());
            e.setId(call.getId());
            e.setTarget(expert);
            e.setTargetId(expert.getId());
            e.setCallAim(call.getCallAim());
            list.add(e);
        }
        return list;
    }
}
